package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.mapper.TradeInfoMapper;
import org.example.pojo.TradeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

@RequestMapping("/trade")
@Controller
public class TrdeInfoController1 {


    @Autowired
    TradeInfoMapper tradeInfoMapper;


    @GetMapping("/list")
    public String listTrades(@RequestParam(required = false) String cardId,
                             @RequestParam(required = false) String tradeType,
                             Model model) {
//        List<TradeInfo> trades = tradeService.queryTrades(cardId, tradeType);

        QueryWrapper<TradeInfo> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(cardId)) {
            queryWrapper.like("card_id",cardId);
        }
        if (!StringUtils.isEmpty(tradeType)) {
            queryWrapper.like("trade_type",tradeType);
        }
        List<TradeInfo> tradeInfos = tradeInfoMapper.selectList(queryWrapper);


        model.addAttribute("tradeList", tradeInfos);
        model.addAttribute("cardId", cardId);
        model.addAttribute("tradeType", tradeType);
        return "/admin/trade/list";
    }


    @GetMapping("/export")
    public void exportToCsv(HttpServletResponse response,@RequestParam(required = false) String cardId,
                            @RequestParam(required = false) String tradeType) throws IOException {
        response.setContentType("text/csv; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=trades.csv");

        QueryWrapper<TradeInfo> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(cardId)) {
            queryWrapper.like("card_id",cardId);
        }
        if (!StringUtils.isEmpty(tradeType)) {
            queryWrapper.like("trade_type",tradeType);
        }
        List<TradeInfo> tradeInfos = tradeInfoMapper.selectList(queryWrapper);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try (PrintWriter writer = response.getWriter()) {
            // 写入 CSV 表头
            writer.println("ID,交易日期,交易类型,交易金额,卡号,备注");

            // 写入每条交易记录
            for (TradeInfo trade : tradeInfos) {
                String format = sdf.format(trade.getTradeDate());
                writer.println(String.format("%d,%s,%s,%.2f,%s,%s",
                        trade.getTradeId(),
                        format,
                        trade.getTradeType(),
                        trade.getTradeMoney(),
                        trade.getCardId(),
                        trade.getRemark() == null ? "" : trade.getRemark().replace(",", " ")
                ));
            }

            writer.flush();
        }
    }
}
